package mortal.game.control
{
    import Message.Game.*;
    import com.gengine.utils.*;
    import flash.events.*;
    import flash.geom.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.rules.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.map.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.pet.info.*;
    import mortal.game.view.petMeet.*;
    import mortal.game.view.petMeet.info.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class PetMeetController extends Controller
    {
        private var _petMeetModule:PetMeetModule;
        private var _petSelectWindow:PetMeetSelectPetWindow;

        public function PetMeetController()
        {
            Dispatcher.addEventListener(PetMeetEventName.OpenPetSelectWindow, this.onOpenPetSelectWindowHandler);
            Dispatcher.addEventListener(PetMeetEventName.ClosePetSelectWindow, this.onClosePetSelectWindowHandler);
            Dispatcher.addEventListener(EventName.PetMeetGetLevyList, this.onPetMeetGetLevyListHandler);
            Dispatcher.addEventListener(EventName.PetMeetGetLevyListSuccess, this.onPetMeetGetLevyListSuccessHandler);
            Dispatcher.addEventListener(EventName.PetMeetStorage, this.onPetMeetStorageHandler);
            Dispatcher.addEventListener(EventName.PetMeetStorageSuccess, this.onPetMeetStorageSuccessHandler);
            Dispatcher.addEventListener(EventName.PetMeetGetBackStoragePet, this.onPetMeetGetBackStoragePetHandler);
            Dispatcher.addEventListener(EventName.PetMeetGetBackStoragePetSuccess, this.onPetMeetGetBackStoragePetSuccessHandler);
            Dispatcher.addEventListener(EventName.PetMeetMatchPet, this.onPetMeetMatchPetHandler);
            Dispatcher.addEventListener(EventName.PetMeetMatchPetSuccess, this.onPetMeetMatchPetSuccessHandler);
            Dispatcher.addEventListener(EventName.PetMeetGetMyPetStorageInfo, this.onPetMeetGetMyPetStorageInfoHandler);
            Dispatcher.addEventListener(EventName.PetMeetGetMyPetStorageInfoSuccess, this.onPetMeetGetMyPetStorageInfoSuccessHandler);
            Dispatcher.addEventListener(EventName.PetBagUpdate, this.onPetBagUpdateHandler);
            
        }

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.onResChanggeHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onResChanggeHandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onResChanggeHandler);
            
        }

        override protected function initView() : IView
        {
            if (this._petMeetModule == null)
            {
                this._petMeetModule = new PetMeetModule();
                this._petMeetModule.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            }
            return this._petMeetModule;
        }

        override public function get view() : IView
        {
            return this.initView();
        }

        private function onAddedToStageHandler(event:Event) : void
        {
            NpcEffectRule.registDialogEffectWindow(this.view);
            this._petMeetModule.showPanelByName();
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetMeetGetMyPetStorageInfo));
            
        }

        private function onOpenPetSelectWindowHandler(event:DataEvent) : void
        {
            var _loc_2:* = this._petMeetModule.x + this._petMeetModule.width;
            if (this._petSelectWindow == null)
            {
                this._petSelectWindow = new PetMeetSelectPetWindow();
            }
            var _loc_3:* = this._petMeetModule.y + this._petMeetModule.height - this._petSelectWindow.height;
            this._petSelectWindow.updateDataProvider(cache.pet.petInfos);
            this._petSelectWindow.show(_loc_2, _loc_3);
            
        }

        private function onClosePetSelectWindowHandler(event:DataEvent) : void
        {
            if (this._petSelectWindow && this._petSelectWindow.isHide == false)
            {
                this._petSelectWindow.hide();
            }
            
        }

        private function onPetMeetGetLevyListHandler(event:DataEvent) : void
        {
            GameProxy.petProxy.getLevyList();
            
        }

        private function onPetMeetGetLevyListSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Array;
            if (this._petMeetModule)
            {
                this._petMeetModule.setIndexPanelListData(_loc_2);
            }
            cache.petMeet.queryStorageArray = _loc_2;
            
        }

        private function onPetMeetStorageHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["petUid"] as String;
            var _loc_3:* = event.data["rgrowth"] as int;
            var _loc_4:* = event.data["rtalent"] as int;
            GameProxy.petProxy.storage(_loc_2, _loc_3, _loc_4);
            
        }

        private function onPetMeetStorageSuccessHandler(event:DataEvent) : void
        {
            cache.petMeet.isHadStoragePet = true;
            var _loc_2:* = event.data as SPetInfo;
            var _loc_3:* = HTMLUtil.addColor(_loc_2.name, PetColor.getTalentColor(_loc_2).color);
            MsgManager.addBroadCast("系统收回" + _loc_3 + "！");
            MsgManager.addBroadCast("报名征友成功！");
            if (this._petMeetModule)
            {
                this._petMeetModule.clearPanelByName(PetMeetPanelName.MeetPanel);
                this._petMeetModule.gotoViewPanel();
                this.onClosePetSelectWindowHandler(null);
            }
            
        }

        private function onPetMeetGetBackStoragePetHandler(event:DataEvent) : void
        {
            GameProxy.petProxy.getBackStoragePet();
            
        }

        private function onPetMeetGetBackStoragePetSuccessHandler(event:DataEvent) : void
        {
            cache.petMeet.isHadStoragePet = false;
            MsgManager.addBroadCast("取回寄存宠物成功！");
            if (this._petMeetModule)
            {
                this._petMeetModule.clearPanelByName(PetMeetPanelName.GetbackPanel);
                this._petMeetModule.gotoIndexPanel();
            }
            
        }

        private function onPetMeetMatchPetHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["playerId"] as int;
            var _loc_3:* = event.data["petUid"] as String;
            var _loc_4:* = event.data["myPetUid"] as String;
            GameProxy.petProxy.matchPet(_loc_2, _loc_3, _loc_4);
            
        }

        private function onPetMeetMatchPetSuccessHandler(event:DataEvent) : void
        {
            cache.petMeet.isHadStoragePet = true;
            MsgManager.addBroadCast("宠物已成功进入繁殖！");
            if (this._petMeetModule)
            {
                this._petMeetModule.gotoViewPanel();
                this._petMeetModule.clearPanelByName(PetMeetPanelName.SirePanel);
                this.onClosePetSelectWindowHandler(null);
            }
            
        }

        private function onPetMeetGetMyPetStorageInfoHandler(event:DataEvent) : void
        {
            GameProxy.petProxy.getMyPetStorageInfo();
            
        }

        private function onPetMeetGetMyPetStorageInfoSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as PetMeetStorageInfo;
            if (_loc_2.petStorage.code != 0)
            {
                cache.petMeet.isHadStoragePet = true;
            }
            else
            {
                cache.petMeet.isHadStoragePet = false;
                
            }
            if (this._petMeetModule)
            {
                this._petMeetModule.updateMyStoragePetPanel(_loc_2);
            }
            
        }

        public function onPetBagUpdateHandler(event:DataEvent) : void
        {
            if (this._petSelectWindow && this._petSelectWindow.isHide == false)
            {
                this._petSelectWindow.updateDataProvider(cache.pet.petInfos);
            }
            
        }

        private function onResChanggeHandler(param1:Object = null) : void
        {
            if (this._petMeetModule && this._petMeetModule.isHide == false)
            {
                this._petMeetModule.updateResourceInfo();
            }
            
        }

        private function onGirdWalkEnd(event:PlayerEvent) : void
        {
            var _loc_3:NPCInfo = null;
            var _loc_4:Point = null;
            var _loc_5:Number = NaN;
            var _loc_2:* = RolePlayer.instance.currentPoint;
            if (this._petMeetModule && this._petMeetModule.isHide == false)
            {
                _loc_3 = cache.dialog.npcInfo;
                if (_loc_3)
                {
                    _loc_4 = GameMapUtil.getPixelPoint(_loc_3.snpc.point.x, _loc_3.snpc.point.y);
                    _loc_5 = Math.sqrt(Math.pow(_loc_4.x - _loc_2.x, 2) + Math.pow(_loc_4.y - _loc_2.y, 2));
                    if (_loc_5 > 120)
                    {
                        this._petMeetModule.hide();
                    }
                }
            }
            
        }

    }
}
